package com.hzpd.hainan.module.usercenter.acticity;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hzpd.hainan.R;
import com.hzpd.hainan.app.App;
import com.hzpd.hainan.app.ToolBarActivity;
import com.hzpd.hainan.config.CODE;
import com.hzpd.hainan.custorm.CircleImageView;
import com.hzpd.hainan.model.event.ChangeNameEvent;
import com.hzpd.hainan.model.usercenter.UserEntity;
import com.hzpd.hainan.module.usercenter.UserCenterFragment;
import com.hzpd.hainan.module.usercenter.acticity.ChangePwActivity;
import com.hzpd.hainan.module.usercenter.dialog.ChangeNameOrSignDialog;
import com.hzpd.hainan.net.Factory;
import com.hzpd.hainan.utils.BM;
import com.hzpd.hainan.utils.CipherUtils;
import com.hzpd.hainan.utils.DeviceUtils;
import com.hzpd.hainan.utils.LogUtils;
import com.hzpd.hainan.utils.ParamUtils;
import com.hzpd.hainan.utils.TUtils;
import com.yanzhenjie.album.Action;
import com.yanzhenjie.album.Album;
import com.yanzhenjie.album.AlbumFile;
import com.yanzhenjie.album.api.widget.Widget;
import com.yanzhenjie.durban.Controller;
import com.yanzhenjie.durban.Durban;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import butterknife.BindView;
import butterknife.OnClick;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.ShareSDK;
import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class AccountSetActivity extends ToolBarActivity {
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.iv_user_account_activity)
    CircleImageView iv_user_account_activity;
    @BindView(R.id.tv_nickname_account_activity)
    TextView tv_nickname_account_activity;

    @BindView(R.id.photo_tv)
    TextView photo_tv;
    @BindView(R.id.nickname_tv)
    TextView nickname_tv;
    @BindView(R.id.sex_tv)
    TextView sex_tv;
    @BindView(R.id.tv_sex_account_activity)
    TextView tv_sex_account_activity;
    @BindView(R.id.birthday_tv)
    TextView birthday_tv;
    @BindView(R.id.tv_brith_account_activity)
    TextView tv_brith_account_activity;
    @BindView(R.id.change_pwd_tv)
    TextView change_pwd_tv;
    @BindView(R.id.phone_num_tv)
    TextView phone_num_tv;
    @BindView(R.id.tv_phone_account_activity)
    TextView tv_phone_account_activity;
    @BindView(R.id.unlogin)
    Button unlogin;
    @Override
    public void initView() {
        if (spu.getLanguage().equals(CODE.ZHONGWEN)) {
            tv_title.setText("账户设置");
            photo_tv.setText("头像");
            nickname_tv.setText("昵称");
            sex_tv.setText("性别");
            tv_sex_account_activity.setText("请选择");
            birthday_tv.setText("出生年月");
            tv_brith_account_activity.setText("请选择");
            change_pwd_tv.setText("修改密码");
            phone_num_tv.setText("手机号");
            tv_phone_account_activity.setText("未绑定");
            unlogin.setText("退出登录");
        }else{
            tv_title.setText("ཨང་རྟགས་སྒྲིག་འགོད།");
            photo_tv.setText("མགོ་པར།");
            nickname_tv.setText("མིང་།");
            sex_tv.setText("ཕོ་མོ།");
            tv_sex_account_activity.setText("གདམ་གསེས།");
            birthday_tv.setText("སྐྱེས་སྐར།");
            tv_brith_account_activity.setText("གདམ་གསེས།");
            change_pwd_tv.setText("གསང་གྲངས་བརྗེ་བ།");
            phone_num_tv.setText("ཁ་པར།");
            tv_phone_account_activity.setText("སྦྲེལ་མེད་པ།");
            unlogin.setText("ཕྱིར་འབུད།");
        }
    }
    private static final int IMAGE_REQUEST_CODE = 450;
    private static final int CAMERA_REQUEST_CODE = 451;
    private static final int RESULT_REQUEST_CODE = 452;
    private String[] items = new String[]{"选择本地图片", "拍照"};
    private String[] items_zang = new String[]{"པར་རིས་འདེམ་པ།", "པར་ལེན།"};
    private File imgFile;
    private final String IMAGE_FILE_NAME = "faceImage.jpg";
    private String[] items_sex= new String[]{"男", "女"};
    private String[] items_sex_zang= new String[]{"ཕོ།", "མོ།"};
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 200:
                    init();
                    break;
            }
        }
    };
    @Override
    public String initToolBarTitle() {
        return null;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);

        init();
        imgFile = getImgPath();
    }



    private void init() {
        if(spu.getUser()!=null) {
            Glide.with(activity).load(spu.getUser().getAvatar_path()).placeholder(R.drawable.pic_touxiang).dontAnimate().into(iv_user_account_activity);
            tv_nickname_account_activity.setText(spu.getUser().getNickname());
        }
        if (spu.getUser().getBirthday() != null) {
            tv_brith_account_activity.setText(spu.getUser().getBirthday());
        }
        if (spu.getUser().getMobile() != null) {
            tv_phone_account_activity.setText(spu.getUser().getMobile());

        }
        if (spu.getUser().getSex() != null) {
            if ("1".equals(spu.getUser().getSex())) {
                if (spu.getLanguage().equals(CODE.ZHONGWEN)) {
                    tv_sex_account_activity.setText("男");
                }else{
                    tv_sex_account_activity.setText("ཕོ།");
                }
            } else if ("2".equals(spu.getUser().getSex())) {
                if (spu.getLanguage().equals(CODE.ZHONGWEN)) {
                    tv_sex_account_activity.setText("女");
                }else{
                    tv_sex_account_activity.setText("མོ།");
                }
            }
        }

    }


    @Override
    public int setMyContentView() {
        return R.layout.activity_account_set;
    }

    @OnClick({R.id.phone_activity_account_set, R.id.phonto_activity_account_set,
            R.id.nickname_activity_account_set,
            R.id.sex_activity_account_set,
            R.id.brith_activity_account_set,
            R.id.changpw_activity_account_set,
    })
    public void Click(View view) {
        switch (view.getId()) {
            case R.id.phonto_activity_account_set:
                if(spu.getUser() != null && spu.getLogintype().equals("2")){
                    TUtils.toast("三方的帐号，不可修改头像");
                }else {
                    changePhoto();
                }
                break;
            case R.id.nickname_activity_account_set:
                if(spu.getUser() != null && spu.getLogintype().equals("2")){
                    TUtils.toast("三方的帐号，不可修改昵称");
                }else {
                    ChangeNameOrSignDialog changeNameDialog = new ChangeNameOrSignDialog(
                            activity, R.style.ChangeNamedialog, 0); // 0为修改昵称 1为修改个人签名
                    changeNameDialog.show();
                }
                break;
            case R.id.sex_activity_account_set:
                if(spu.getUser() != null && spu.getLogintype().equals("2")){
                    TUtils.toast("三方的帐号，不可修改性别");
                }else {
                    changeSex();
                }
                break;
            case R.id.brith_activity_account_set:
                if(spu.getUser() != null && spu.getLogintype().equals("2")){
                    TUtils.toast("三方的帐号，不可修改生日");
                }else {
                    DateShowDialog();
                }
                break;
            case R.id.changpw_activity_account_set:
                Intent intent=new Intent(activity,ChangePwActivity.class);
                startActivity(intent);
                break;
            case R.id.phone_activity_account_set:
                //绑定手机
                break;
        }
    }

    private void changeSex() {
        if (spu.getLanguage().equals(CODE.ZHONGWEN)) {
            new AlertDialog.Builder(activity).
                    setTitle("修改性别")
                    .setItems(items_sex, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            switch (i) {
                                case 0:
                                    ChangeType("sex", "1");

                                    break;
                                case 1:
                                    ChangeType("sex", "2");

                                    break;
                            }
                        }
                    }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    disMissDialog();
                }
            }).setCancelable(true).show();
        }else{
            new AlertDialog.Builder(activity).
                    setTitle("ཕོ་མོ།")
                    .setItems(items_sex_zang, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            switch (i) {
                                case 0:
                                    ChangeType("sex", "1");

                                    break;
                                case 1:
                                    ChangeType("sex", "2");

                                    break;
                            }
                        }
                    }).setNegativeButton("ཕྱིར་འཐེན།", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    disMissDialog();
                }
            }).setCancelable(true).show();
        }
    }

    public void DateShowDialog(){
        Calendar c=Calendar.getInstance();
        DatePickerDialog datePickerDialog=new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {

                String brithday=i+"-"+(i1+1)+"-"+i2;
                ChangeType("birthday", brithday);
            }
        },c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH));
        datePickerDialog.setCancelable(true);
        datePickerDialog.show();
    }

    private void ChangeType(String type, String value) {
//        BM bm=new BM();
//        try {
//            String a=bm.toUTF_8(value);
//
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }
        Map<String,String> map=new HashMap<>();
        map.put(type,value);
        map.put("uid",spu.getUser().getUid());
        LogUtils.e("uid"+spu.getUser().getUid());

        Factory.provideHttpService().changeUser(ParamUtils.getRequestParam(map))
                .onBackpressureBuffer()
                .subscribeOn(Schedulers.io())
                .filter(new Func1<UserEntity, Boolean>() {
                    @Override
                    public Boolean call(UserEntity userEntity) {
                        LogUtils.e("photo",userEntity.code);
//                        if ("200".equals(userEntity.code)) {
//
//                            return true;
//                        } else {
//                            TUtils.toast(userEntity.msg);
//                            return true;
//                        }
                        return true;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<UserEntity>() {
                    @Override
                    public void call(UserEntity userEntity) {
                        if ("200".equals(userEntity.code)) {
                            TUtils.toast("修改成功");
                            spu.setUser(userEntity.data);
                            LogUtils.e("birth"+spu.getUser().getBirthday());
                            Message msg=handler.obtainMessage();
                            msg.what=200;
                            handler.sendMessage(msg);
                        }else {
                            TUtils.toast(userEntity.msg);
                        }

                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        LogUtils.e("throwable------"+throwable.getMessage());
                    }
                });
    }

    @OnClick(R.id.unlogin)
    public void UnLogin(View view){
        if (ShareSDK.getPlatformList() != null) {
            for (Platform pl : ShareSDK.getPlatformList()) {
                if (pl.isAuthValid()) {
                    pl.SSOSetting(true);
                    pl.removeAccount(true);
                }
            }
        }
//        TUtils.toast(spu.getUser().getWechat());
        spu.setUser(null);
//        LogUtils.e(spu.getUser().toString());
        //EventUtils.getEventUrl();
        Intent intent = new Intent();
        if (view != null) {
            intent.setAction(UserCenterFragment.ACTION_QUIT);

        } else {
            intent.setAction(UserCenterFragment.ACTION_QUIT_LOGIN);
        }
        activity.sendBroadcast(intent);
        activity.finish();

    }
    @Subscribe
    public void onEventMainThread(ChangeNameEvent event) {
        LogUtils.i("登录成功");

        boolean nameChange = event.isNameChange();

            tv_nickname_account_activity.setText(spu.getUser().getNickname());

    }

    public void changePhoto() {
        if (spu.getLanguage().equals(CODE.ZHONGWEN)) {
            new AlertDialog.Builder(activity)
                    .setTitle("设置头像")
                    .setItems(items, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            switch (which) {
                                case 0: {
                                    Widget widget = Widget.newDarkBuilder(AccountSetActivity.this)
                                            .title("选择图片") // 标题。
                                            .statusBarColor(ContextCompat.getColor(activity, R.color.black)) // 状态栏颜色。
                                            .toolBarColor(ContextCompat.getColor(activity, R.color.colorPrimary))// Toolbar颜色。
                                            .navigationBarColor(ContextCompat.getColor(activity, R.color.colorPrimary)) // Android5.0+的虚拟导航栏颜色。
                                            .build();
                                    Album.image(activity) // Image selection.
                                            .multipleChoice()
                                            .requestCode(200)
                                            .camera(true)
                                            .columnCount(3)
                                            .selectCount(1)
                                            .widget(widget)
                                            .onResult(new Action<ArrayList<AlbumFile>>() {
                                                @Override
                                                public void onAction(int requestCode, @NonNull ArrayList<AlbumFile> result) {
                                                    startPhotoZoom(result.get(0).getPath());
                                                }
                                            })
                                            .onCancel(new Action<String>() {
                                                @Override
                                                public void onAction(int requestCode, @NonNull String result) {
                                                }
                                            })
                                            .start();
                                }
                                break;
                                case 1: {
                                    Album.camera(activity) // 相机功能。
                                            .image() // 拍照。
                                            .requestCode(CAMERA_REQUEST_CODE)
                                            .onResult(new Action<String>() {
                                                @Override
                                                public void onAction(int requestCode, @NonNull String result) {
                                                    LogUtils.e("string--" + result);
                                                    startPhotoZoom(result);
                                                }
                                            })
                                            .onCancel(new Action<String>() {
                                                @Override
                                                public void onAction(int requestCode, @NonNull String result) {
                                                    TUtils.toast("取消");
                                                }
                                            })
                                            .start();
                                }
                                break;
                            }
                        }
                    })
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    }).show();
        } else {
            new AlertDialog.Builder(activity)
                    .setTitle("མགོ་པར་འཇོག་པ།")
                    .setItems(items_zang, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            switch (which) {
                                case 0: {
                                    Widget widget = Widget.newDarkBuilder(AccountSetActivity.this)
                                            .title("པར་རིས།") // 标题。
                                            .statusBarColor(ContextCompat.getColor(activity, R.color.black)) // 状态栏颜色。
                                            .toolBarColor(ContextCompat.getColor(activity, R.color.colorPrimary))// Toolbar颜色。
                                            .navigationBarColor(ContextCompat.getColor(activity, R.color.colorPrimary)) // Android5.0+的虚拟导航栏颜色。
                                            .build();
                                    Album.image(activity) // Image selection.
                                            .multipleChoice()
                                            .requestCode(200)
                                            .camera(true)
                                            .columnCount(3)
                                            .selectCount(1)
                                            .widget(widget)
                                            .onResult(new Action<ArrayList<AlbumFile>>() {
                                                @Override
                                                public void onAction(int requestCode, @NonNull ArrayList<AlbumFile> result) {
                                                    startPhotoZoom(result.get(0).getPath());
                                                }
                                            })
                                            .onCancel(new Action<String>() {
                                                @Override
                                                public void onAction(int requestCode, @NonNull String result) {
                                                }
                                            })
                                            .start();
                                }
                                break;
                                case 1: {
                                    Album.camera(activity) // 相机功能。
                                            .image() // 拍照。
                                            .requestCode(CAMERA_REQUEST_CODE)
                                            .onResult(new Action<String>() {
                                                @Override
                                                public void onAction(int requestCode, @NonNull String result) {
                                                    LogUtils.e("string--" + result);
                                                    startPhotoZoom(result);
                                                }
                                            })
                                            .onCancel(new Action<String>() {
                                                @Override
                                                public void onAction(int requestCode, @NonNull String result) {
                                                    TUtils.toast("ཕྱིར་འཐེན།");
                                                }
                                            })
                                            .start();
                                }
                                break;
                            }
                        }
                    })
                    .setNegativeButton("ཕྱིར་འཐེན།", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    }).show();
        }
    }
    public void startPhotoZoom(String imagePathList){
        Durban.with(activity)
                .title("图片裁剪")
                .statusBarColor(ContextCompat.getColor(this, R.color.black))
                .toolBarColor(ContextCompat.getColor(this, R.color.colorPrimary))
                .navigationBarColor(ContextCompat.getColor(this, R.color.colorPrimary))
                .inputImagePaths(imagePathList)
                .maxWidthHeight(200, 200)
                .aspectRatio(1, 1)
                .compressFormat(Durban.COMPRESS_JPEG)
                .compressQuality(90)
                .gesture(Durban.GESTURE_ALL)
                .controller(
                        Controller.newBuilder() // Create Builder of Controller.
                                .enable(false) // Enable the control panel.
                                .rotation(true) // Rotation button.
                                .rotationTitle(true) // Rotation button title.
                                .scale(true) // Scale button.
                                .scaleTitle(true) // Scale button title.
                                .build()) // Create Controller Config.
                .requestCode(RESULT_REQUEST_CODE)
                .start();
    }
//    public void changePhoto() {
//        if (spu.getLanguage().equals(CODE.ZHONGWEN)) {
//            new AlertDialog.Builder(activity)
//                    .setTitle("设置头像")
//                    .setItems(items, new DialogInterface.OnClickListener() {
//                        @Override
//                        public void onClick(DialogInterface dialog, int which) {
//                            switch (which) {
//                                case 0: {
//                                    Intent intentFromGallery = new Intent();
//                                    intentFromGallery.setType("image/*"); // 设置文件类型
//                                    intentFromGallery
//                                            .setAction(Intent.ACTION_GET_CONTENT);
//                                    activity.startActivityForResult(intentFromGallery,
//                                            IMAGE_REQUEST_CODE);
//
//                                }
//                                break;
//                                case 1: {
//                                    Intent intentFromCapture = new Intent(
//                                            MediaStore.ACTION_IMAGE_CAPTURE);
//                                    intentFromCapture.putExtra(MediaStore.EXTRA_OUTPUT,
//                                            Uri.fromFile(imgFile));
//                                    activity.startActivityForResult(intentFromCapture,
//                                            CAMERA_REQUEST_CODE);
//
//                                }
//                                break;
//                            }
//                        }
//                    })
//                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
//                        @Override
//                        public void onClick(DialogInterface dialog, int which) {
//                            dialog.dismiss();
//                        }
//                    }).show();
//        }else{
//            new AlertDialog.Builder(activity)
//                    .setTitle("མགོ་པར་འཇོག་པ།")
//                    .setItems(items_zang, new DialogInterface.OnClickListener() {
//                        @Override
//                        public void onClick(DialogInterface dialog, int which) {
//                            switch (which) {
//                                case 0: {
//                                    Intent intentFromGallery = new Intent();
//                                    intentFromGallery.setType("image/*"); // 设置文件类型
//                                    intentFromGallery
//                                            .setAction(Intent.ACTION_GET_CONTENT);
//                                    activity.startActivityForResult(intentFromGallery,
//                                            IMAGE_REQUEST_CODE);
//
//                                }
//                                break;
//                                case 1: {
//                                    Intent intentFromCapture = new Intent(
//                                            MediaStore.ACTION_IMAGE_CAPTURE);
//                                    intentFromCapture.putExtra(MediaStore.EXTRA_OUTPUT,
//                                            Uri.fromFile(imgFile));
//                                    activity.startActivityForResult(intentFromCapture,
//                                            CAMERA_REQUEST_CODE);
//
//                                }
//                                break;
//                            }
//                        }
//                    })
//                    .setNegativeButton("ཕྱིར་འཐེན།", new DialogInterface.OnClickListener() {
//                        @Override
//                        public void onClick(DialogInterface dialog, int which) {
//                            dialog.dismiss();
//                        }
//                    }).show();
//        }
//    }


    public File getImgPath() {
        File cacheFileDir;
        if (Environment.MEDIA_MOUNTED.equals(Environment
                .getExternalStorageState())
                || !Environment.isExternalStorageRemovable()) {
            cacheFileDir = Environment.getExternalStorageDirectory();
        } else {
            cacheFileDir = activity.getApplicationContext().getCacheDir();
        }
        if (null != cacheFileDir && !cacheFileDir.exists()) {
            cacheFileDir.mkdirs();
        }
        File cacheFile = App.getFile(cacheFileDir.getAbsolutePath()
                + File.separator + "cyol" + File.separator + IMAGE_FILE_NAME);

        return cacheFile;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        LogUtils.i("requestCode-->" + requestCode + "  resultCode-->"
                + resultCode);
        switch (requestCode) {
            case IMAGE_REQUEST_CODE: {
                if (null != data) {
                    startPhotoZoom(data.getData());
                }
            }
            break;
            case CAMERA_REQUEST_CODE: {
                startPhotoZoom();
            }
            break;
            case RESULT_REQUEST_CODE: {
                if (data != null) {
                    Bundle extras = data.getExtras();
                    if (extras != null) {
                        ArrayList<String> mImageList = Durban.parseResult(data);
                        if(mImageList != null && mImageList.size()>0) {
                            Bitmap bitmap = BitmapFactory.decodeFile(mImageList.get(0));
                            uploadPhoto(bitmap);
                        }
                    } else {
                        LogUtils.i("extras null");
                    }
                }
            }
            break;
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

    private void uploadPhoto(Bitmap photo) {
        try {
            photo.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(
                    imgFile));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        Map<String, String> map = new HashMap<>();
        map.put("avatar", CipherUtils.base64Encode(photo));
        map.put("uid",spu.getUser().getUid());

        Factory.provideHttpService()
                .changeUser_PHOTO(ParamUtils.getRequestParam(map))
                .onBackpressureBuffer()
                .subscribeOn(Schedulers.io())
                .filter(new Func1<UserEntity, Boolean>() {
                    @Override
                    public Boolean call(UserEntity userEntity) {
                        LogUtils.e("photo",userEntity.code);
                        if ("200".equals(userEntity.code)) {

                            return true;
                        } else {
                            TUtils.toast(userEntity.msg);
                        }
                        return false;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<UserEntity>() {
                    @Override
                    public void call(UserEntity userEntity) {
                        if ("200".equals(userEntity.code)) {
                            TUtils.toast("修改成功");
                            spu.setUser(userEntity.data);
                            Glide.with(activity).load(spu.getUser().getAvatar_path()).placeholder(R.drawable.pic_touxiang).dontAnimate().into(iv_user_account_activity);
                            Intent intent = new Intent();
                            intent.setAction(UserCenterFragment.ACTION_USER);
                            activity.sendBroadcast(intent);
                        }

                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {

                    }
                });
    }

    public void startPhotoZoom() {
        Uri uri = Uri.fromFile(imgFile);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            uri = FileProvider.getUriForFile(AccountSetActivity.this, "com.hzpd.hainan.fileprovider", imgFile);//通过FileProvider创建一个content类型的Uri
        }
        startPhotoZoom(uri);
    }

    // 图片裁剪
    public void startPhotoZoom(Uri uri) {
        if (null == uri) {
            return;
        }
        Intent intent = new Intent("com.android.camera.action.CROP");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        }
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("outputX", 320);
        intent.putExtra("outputY", 320);
        intent.putExtra("return-data", true);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imgFile));
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        activity.startActivityForResult(intent, RESULT_REQUEST_CODE);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
